package com.huel.controller;


import com.huel.common.Result;
import com.huel.domain.Emp;
import com.huel.dto.Page;
import com.huel.dto.PageParams;
import com.huel.enums.ResultEnums;
import com.huel.service.EmpService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("emp")
public class EmpController {

    @Resource
    private EmpService empService;


    /*
    *   多条件分页查询
    * */
    @PostMapping("getEmpPageList")
    public Result<Page> getEmpPageListByParams(@RequestBody PageParams pageParams){
        Page page=empService.getEmpPageListByParams(pageParams);
        return new Result<>(page);
    }

    @PostMapping("save")
    public Result<?> saveEmp(@RequestBody Emp emp){
        Result<?> result=new Result<>();
        try {
            empService.insertSelective(emp);
        } catch (Exception e) {
            e.printStackTrace();
            //result= new Result<>(40000,e.getMessage());
            result.setCode(40000);
            result.setMsg(e.getMessage());
        }
        return  result;
    }


    @GetMapping("getEmpByEmpId/{empId}")
    public Result<Emp> getEmpByEmpId(@PathVariable Integer empId){
        Emp emp= null;
        Result<Emp> result=new Result<>();
        try {
            emp = empService.selectByPrimaryKey(empId);
            result.setData(emp);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultEnums.ERROR.getCode());
            result.setMsg(e.getMessage());
        }
        return result;
    }


    @PutMapping("update")
    public Result<?> updateEmp(@RequestBody  Emp emp){
        Result<?> result=new Result<>();
        try {
            empService.updateByPrimaryKeySelective(emp);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(40000);
            result.setMsg(e.getMessage());
        }
        return  result;
    }


    @DeleteMapping("delete/{empId}")
    public Result<?> deleteEmpByEmpId(@PathVariable Integer empId){
        Result<?> result=new Result<>();
        try {
            empService.deleteByPrimaryKey(empId);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(40000);
            result.setMsg(e.getMessage());
        }
        return  result;
    }












}
